/*
 * SimulatorFrame.java
 *
 * Created on Mar 21, 2009, 6:45:04 PM
 */
package laf.socf.simulator.gui;

import java.awt.BorderLayout;
import java.awt.Point;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Observable;
import java.util.Observer;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.filechooser.FileFilter;
import laf.socf.simulator.sensors.Compass;
import laf.socf.simulator.sensors.GPS;
import laf.socf.simulator.sensors.MetalDetector;
import laf.socf.simulator.sensors.Navigation;
import laf.socf.simulator.sensors.Sensor;
import laf.socf.simulator.sensors.Sonar;

/**
 *
 * @author L.A. Fernando
 */
public class SimulatorFrame extends javax.swing.JFrame implements Observer {

    /** Creates new form SimulatorFrame */
    private SimContext simContext = null;
    private int robotCount = 1;
    private int sonarRightCount = 1;
    private int sonarFrontCount = 1;
    private int navigationCount = 1;
    private int gpsCount = 1;
    private int compassCount = 1;
    private int metalDetectorCount = 1;

    public SimulatorFrame() {
        initComponents();

        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        this.simContext = new SimContext();
        this.simContext.setObserver(this);
        this.mainPane.add(simContext, BorderLayout.CENTER);

        this.stopButton.setEnabled(false);
        this.startButton.setEnabled(true);
    }

    public void update(Observable src, Object data) {
        if (data instanceof String) {
            this.info(data.toString());
        } else if (data instanceof Point) {
            Point p = (Point) data;
            this.pointLabel.setText("[x:" + (int) p.getX() + " y:" + (int) p.getY() + "]");
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        toolBar = new javax.swing.JToolBar();
        newButton = new javax.swing.JButton();
        openButton = new javax.swing.JButton();
        saveButton = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JToolBar.Separator();
        startButton = new javax.swing.JButton();
        stopButton = new javax.swing.JButton();
        jSeparator2 = new javax.swing.JToolBar.Separator();
        exitButton = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        robotButton = new javax.swing.JButton();
        compassSensor = new javax.swing.JButton();
        gpsSensor = new javax.swing.JButton();
        brickWall = new javax.swing.JButton();
        navigationButton = new javax.swing.JButton();
        sonarRightSensor = new javax.swing.JButton();
        sonarFrontSensor = new javax.swing.JButton();
        metalDetector = new javax.swing.JButton();
        mine = new javax.swing.JButton();
        mainPane = new javax.swing.JPanel();
        southPane = new javax.swing.JPanel();
        statusBar = new javax.swing.JLabel();
        pointLabel = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("SOCF Simulator");
        setBackground(new java.awt.Color(0, 0, 0));
        setIconImage(new ImageIcon(getClass().getResource("/images/window-icon.png")).getImage());

        toolBar.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        toolBar.setFloatable(false);
        toolBar.setRollover(true);

        newButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/new.png"))); // NOI18N
        newButton.setToolTipText("New");
        newButton.setFocusable(false);
        newButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        newButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        newButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                newButtonActionPerformed(evt);
            }
        });
        toolBar.add(newButton);

        openButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/open.png"))); // NOI18N
        openButton.setToolTipText("Open");
        openButton.setFocusable(false);
        openButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        openButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        openButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                openButtonActionPerformed(evt);
            }
        });
        toolBar.add(openButton);

        saveButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/save.png"))); // NOI18N
        saveButton.setToolTipText("Save");
        saveButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveButtonActionPerformed(evt);
            }
        });
        toolBar.add(saveButton);
        toolBar.add(jSeparator1);

        startButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/play.png"))); // NOI18N
        startButton.setToolTipText("Start");
        startButton.setFocusable(false);
        startButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        startButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        startButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                startButtonActionPerformed(evt);
            }
        });
        toolBar.add(startButton);

        stopButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/stop.png"))); // NOI18N
        stopButton.setToolTipText("Stop");
        stopButton.setFocusable(false);
        stopButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        stopButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        stopButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                stopButtonActionPerformed(evt);
            }
        });
        toolBar.add(stopButton);
        toolBar.add(jSeparator2);

        exitButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/exit.png"))); // NOI18N
        exitButton.setToolTipText("Exit");
        exitButton.setFocusable(false);
        exitButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        exitButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        exitButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                exitButtonActionPerformed(evt);
            }
        });
        toolBar.add(exitButton);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Toolbox"));

        robotButton.setText("Robot");
        robotButton.setFocusable(false);
        robotButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        robotButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        robotButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                robotButtonActionPerformed(evt);
            }
        });

        compassSensor.setText("Compass Sensor");
        compassSensor.setFocusable(false);
        compassSensor.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        compassSensor.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        compassSensor.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                compassSensorActionPerformed(evt);
            }
        });

        gpsSensor.setText("GPS Sensor");
        gpsSensor.setFocusable(false);
        gpsSensor.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        gpsSensor.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        gpsSensor.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                gpsSensorActionPerformed(evt);
            }
        });

        brickWall.setText("Brick Wall");
        brickWall.setFocusable(false);
        brickWall.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        brickWall.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        brickWall.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                brickWallActionPerformed(evt);
            }
        });

        navigationButton.setText("Navigation");
        navigationButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                navigationButtonActionPerformed(evt);
            }
        });

        sonarRightSensor.setText("Sonar Sensor - Right");
        sonarRightSensor.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                sonarRightSensorActionPerformed(evt);
            }
        });

        sonarFrontSensor.setText("Sonar Sensor - Front");
        sonarFrontSensor.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                sonarFrontSensorActionPerformed(evt);
            }
        });

        metalDetector.setText("Metal Detector");
        metalDetector.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                metalDetectorActionPerformed(evt);
            }
        });

        mine.setText("Mine");
        mine.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mineActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(robotButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(brickWall)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(mine)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(gpsSensor)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(metalDetector))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(navigationButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(compassSensor)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(sonarRightSensor)
                        .addGap(6, 6, 6)
                        .addComponent(sonarFrontSensor)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(compassSensor)
                    .addComponent(navigationButton)
                    .addComponent(sonarRightSensor)
                    .addComponent(sonarFrontSensor))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(brickWall)
                    .addComponent(gpsSensor)
                    .addComponent(robotButton)
                    .addComponent(metalDetector)
                    .addComponent(mine))
                .addContainerGap())
        );

        mainPane.setLayout(new java.awt.BorderLayout());

        southPane.setFont(new java.awt.Font("DejaVu Sans", 0, 8)); // NOI18N

        statusBar.setFont(new java.awt.Font("DejaVu Sans", 0, 10)); // NOI18N

        pointLabel.setFont(new java.awt.Font("DejaVu Sans", 0, 10)); // NOI18N

        javax.swing.GroupLayout southPaneLayout = new javax.swing.GroupLayout(southPane);
        southPane.setLayout(southPaneLayout);
        southPaneLayout.setHorizontalGroup(
            southPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, southPaneLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(statusBar, javax.swing.GroupLayout.DEFAULT_SIZE, 511, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(pointLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 118, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        southPaneLayout.setVerticalGroup(
            southPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, southPaneLayout.createSequentialGroup()
                .addGroup(southPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(pointLabel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 15, Short.MAX_VALUE)
                    .addComponent(statusBar, javax.swing.GroupLayout.DEFAULT_SIZE, 15, Short.MAX_VALUE))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(toolBar, javax.swing.GroupLayout.DEFAULT_SIZE, 647, Short.MAX_VALUE)
            .addComponent(southPane, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(91, Short.MAX_VALUE))
            .addComponent(mainPane, javax.swing.GroupLayout.DEFAULT_SIZE, 647, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(toolBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(mainPane, javax.swing.GroupLayout.DEFAULT_SIZE, 333, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addComponent(southPane, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void exitButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exitButtonActionPerformed
        System.exit(0);
    }//GEN-LAST:event_exitButtonActionPerformed

    private void gpsSensorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_gpsSensorActionPerformed
        Robot robot = this.simContext.getSelectedRobot();
        if (robot != null) {
            Sensor sensor = null;
            try {
                sensor = new GPS(robot, this.gpsCount);
                this.gpsCount++;
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            robot.addSensor(sensor);
            info("Sensor: " + sensor.getSensorName() + " added to Robot: " + robot.getRobotName());
        }
    }//GEN-LAST:event_gpsSensorActionPerformed

    private void robotButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_robotButtonActionPerformed
        Robot robot = new Robot(this.simContext, "Robot " + this.robotCount);
        this.robotCount++;
        this.simContext.addRobot(robot);
        this.repaint();
    }//GEN-LAST:event_robotButtonActionPerformed

    private void brickWallActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_brickWallActionPerformed
        BrickWall brick = new BrickWall(this.simContext);
        this.simContext.addBrick(brick);
        this.repaint();
    }//GEN-LAST:event_brickWallActionPerformed

    private void saveButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveButtonActionPerformed
        try {
            JFileChooser fc = new JFileChooser();
            fc.setFileFilter(new SimFileFilter());
            if (fc.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) {
                String filename = fc.getSelectedFile().getAbsolutePath();
                if (!filename.endsWith(".sim")) {
                    filename += ".sim";
                }
                FileOutputStream fileOut = new FileOutputStream(filename);
                ObjectOutputStream objOut = new ObjectOutputStream(fileOut);
                objOut.writeObject(this.simContext);
                objOut.close();
                fileOut.close();
                this.info("Simulation Saved.");
            }
        } catch (Exception e) {
            this.info(e.getMessage());
        }
    }//GEN-LAST:event_saveButtonActionPerformed

    private void openButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_openButtonActionPerformed
        try {
            JFileChooser fc = new JFileChooser();
            fc.setFileFilter(new SimFileFilter());
            if (fc.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
                FileInputStream fileIn = new FileInputStream(fc.getSelectedFile());
                ObjectInputStream objIn = new ObjectInputStream(fileIn);
                this.simContext = (SimContext) objIn.readObject();
                this.simContext.setObserver(this);
                this.mainPane.removeAll();
                this.mainPane.add(this.simContext, BorderLayout.CENTER);
                this.initSimContext();
                this.repaint();
                objIn.close();
                fileIn.close();
                this.info("Simulation Loaded. ");
            }
        } catch (Exception e) {
            this.info(e.getMessage());
        }
    }//GEN-LAST:event_openButtonActionPerformed

    private void initSimContext() throws Exception {
        for (Robot robot : this.simContext.getRobots()) {
            for (Sensor sensor : robot.getSensors()) {
                sensor.init();
            }
        }
    }

    private void newButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_newButtonActionPerformed
        this.simContext = new SimContext();
        this.simContext.setObserver(this);
        this.mainPane.removeAll();
        this.mainPane.add(this.simContext, BorderLayout.CENTER);
        this.mainPane.revalidate();
        this.info("");
    }//GEN-LAST:event_newButtonActionPerformed

    private void startButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_startButtonActionPerformed
        this.simContext.startRobots();
        this.startButton.setEnabled(false);
        this.stopButton.setEnabled(true);
        this.info("Simulation Started.");
    }//GEN-LAST:event_startButtonActionPerformed

    private void stopButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_stopButtonActionPerformed
        this.simContext.stopRobots();
        this.startButton.setEnabled(true);
        this.stopButton.setEnabled(false);
        this.info("Simulation Stopped.");
    }//GEN-LAST:event_stopButtonActionPerformed

    private void navigationButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_navigationButtonActionPerformed
        Robot robot = this.simContext.getSelectedRobot();
        if (robot != null) {
            Sensor sensor = null;
            try {
                sensor = new Navigation(robot, this.navigationCount);
                this.navigationCount++;
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            robot.addSensor(sensor);
            info("Sensor: " + sensor.getSensorName() + " added to Robot: " + robot.getRobotName());
        }
    }//GEN-LAST:event_navigationButtonActionPerformed

    private void compassSensorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_compassSensorActionPerformed
        Robot robot = this.simContext.getSelectedRobot();
        if (robot != null) {
            Sensor sensor = null;
            try {
                sensor = new Compass(robot, this.compassCount);
                this.compassCount++;
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            robot.addSensor(sensor);
            info("Sensor: " + sensor.getSensorName() + " added to Robot: " + robot.getRobotName());
        }
    }//GEN-LAST:event_compassSensorActionPerformed

    private void sonarRightSensorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_sonarRightSensorActionPerformed
        Robot robot = this.simContext.getSelectedRobot();
        if (robot != null) {
            Sensor sensor = null;
            try {
                sensor = new Sonar(robot, Sonar.TYPE_RIGHT, this.sonarRightCount);
                this.sonarRightCount++;
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            robot.addSensor(sensor);
            info("Sensor: " + sensor.getSensorName() + " added to Robot: " + robot.getRobotName());
        }
    }//GEN-LAST:event_sonarRightSensorActionPerformed

    private void sonarFrontSensorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_sonarFrontSensorActionPerformed
        Robot robot = this.simContext.getSelectedRobot();
        if (robot != null) {
            Sensor sensor = null;
            try {
                sensor = new Sonar(robot, Sonar.TYPE_FRONT, this.sonarFrontCount);
                this.sonarFrontCount++;
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            robot.addSensor(sensor);
            info("Sensor: " + sensor.getSensorName() + " added to Robot: " + robot.getRobotName());
        }
    }//GEN-LAST:event_sonarFrontSensorActionPerformed

    private void mineActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mineActionPerformed
        Mine minex = new Mine(this.simContext);
        this.simContext.addMine(minex);
        this.repaint();
}//GEN-LAST:event_mineActionPerformed

    private void metalDetectorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_metalDetectorActionPerformed
        Robot robot = this.simContext.getSelectedRobot();
        if (robot != null) {
            Sensor sensor = null;
            try {
                sensor = new MetalDetector(robot, this.metalDetectorCount);
                this.metalDetectorCount++;
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            robot.addSensor(sensor);
            info("Sensor: " + sensor.getSensorName() + " added to Robot: " + robot.getRobotName());
        }
    }//GEN-LAST:event_metalDetectorActionPerformed

    private void info(String msg) {
        this.statusBar.setText(msg);
    }

    public class SimFileFilter extends FileFilter {

        public String getDescription() {
            return ".sim files";
        }

        public boolean accept(File file) {
            if (file.isDirectory() || file.getName().endsWith(".sim")) {
                return true;
            } else {
                return false;
            }
        }
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new SimulatorFrame().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton brickWall;
    private javax.swing.JButton compassSensor;
    private javax.swing.JButton exitButton;
    private javax.swing.JButton gpsSensor;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JToolBar.Separator jSeparator1;
    private javax.swing.JToolBar.Separator jSeparator2;
    private javax.swing.JPanel mainPane;
    private javax.swing.JButton metalDetector;
    private javax.swing.JButton mine;
    private javax.swing.JButton navigationButton;
    private javax.swing.JButton newButton;
    private javax.swing.JButton openButton;
    private javax.swing.JLabel pointLabel;
    private javax.swing.JButton robotButton;
    private javax.swing.JButton saveButton;
    private javax.swing.JButton sonarFrontSensor;
    private javax.swing.JButton sonarRightSensor;
    private javax.swing.JPanel southPane;
    private javax.swing.JButton startButton;
    private javax.swing.JLabel statusBar;
    private javax.swing.JButton stopButton;
    private javax.swing.JToolBar toolBar;
    // End of variables declaration//GEN-END:variables
}
